3 use MediaWiki\Shell\Command
;
4 use Wikimedia\TestingAccessWrapper
;
7 * @covers \MediaWiki\Shell\Command
10 class CommandTest
extends PHPUnit_Framework_TestCase
{
11 private function requirePosix() {
12 if ( wfIsWindows() ) {
13 $this->markTestSkipped( 'This test requires a POSIX environment.' );
18 * @dataProvider provideExecute
20 public function testExecute( $commandInput, $expectedExitCode, $expectedOutput ) {
21 $this->requirePosix();
23 $command = new Command();
25 ->params( $commandInput )
28 $this->assertSame( $expectedExitCode, $result->getExitCode() );
29 $this->assertSame( $expectedOutput, $result->getStdout() );
32 public function provideExecute() {
34 'success status' => [ 'true', 0, '' ],
35 'failure status' => [ 'false', 1, '' ],
36 'output' => [ [ 'echo', '-n', 'x', '>', 'y' ], 0, 'x > y' ],
40 public function testEnvironment() {
41 $this->requirePosix();
43 $command = new Command();
45 ->params( [ 'printenv', 'FOO' ] )
46 ->environment( [ 'FOO' => 'bar' ] )
48 $this->assertSame( "bar\n", $result->getStdout() );
51 public function testStdout() {
52 $this->requirePosix();
54 $command = new Command();
57 ->params( 'bash', '-c', 'echo ThisIsStderr 1>&2' )
60 $this->assertNotContains( 'ThisIsStderr', $result->getStdout() );
61 $this->assertEquals( "ThisIsStderr\n", $result->getStderr() );
64 public function testStdoutRedirection() {
65 $this->requirePosix();
67 $command = new Command();
70 ->params( 'bash', '-c', 'echo ThisIsStderr 1>&2' )
71 ->includeStderr( true )
74 $this->assertEquals( "ThisIsStderr\n", $result->getStdout() );
75 $this->assertNull( $result->getStderr() );
78 public function testOutput() {
81 $this->requirePosix();
84 $command = new Command();
86 ->params( [ 'ls', 'index.php' ] )
88 $this->assertRegExp( '/^index.php$/m', $result->getStdout() );
89 $this->assertSame( null, $result->getStderr() );
91 $command = new Command();
93 ->params( [ 'ls', 'index.php', 'no-such-file' ] )
96 $this->assertRegExp( '/^index.php$/m', $result->getStdout() );
97 $this->assertRegExp( '/^.+no-such-file.*$/m', $result->getStdout() );
98 $this->assertSame( null, $result->getStderr() );
100 $command = new Command();
102 ->params( [ 'ls', 'index.php', 'no-such-file' ] )
104 $this->assertRegExp( '/^index.php$/m', $result->getStdout() );
105 $this->assertRegExp( '/^.+no-such-file.*$/m', $result->getStderr() );
109 * Test that null values are skipped by params() and unsafeParams()
111 public function testNullsAreSkipped() {
112 $command = TestingAccessWrapper
::newFromObject( new Command
);
113 $command->params( 'echo', 'a', null, 'b' );
114 $command->unsafeParams( 'c', null, 'd' );
115 $this->assertEquals( "'echo' 'a' 'b' c d", $command->command
);
118 public function testT69870() {
119 $commandLine = wfIsWindows()
121 ?
( 'for /l %i in (1, 1, 1001) do @echo ' . str_repeat( '*', 331 ) )
122 : 'printf "%-333333s" "*"';
124 // Test several times because it involves a race condition that may randomly succeed or fail
125 for ( $i = 0; $i < 10; $i++
) {
126 $command = new Command();
127 $output = $command->unsafeParams( $commandLine )
130 $this->assertEquals( 333333, strlen( $output ) );
134 public function testLogStderr() {
135 $this->requirePosix();
137 $logger = new TestLogger( true, function ( $message, $level, $context ) {
138 return $level === Psr\Log\LogLevel
::ERROR ?
'1' : null;
140 $command = new Command();
141 $command->setLogger( $logger );
142 $command->params( 'bash', '-c', 'echo ThisIsStderr 1>&2' );
144 $this->assertEmpty( $logger->getBuffer() );
146 $command = new Command();
147 $command->setLogger( $logger );
148 $command->logStderr();
149 $command->params( 'bash', '-c', 'echo ThisIsStderr 1>&2' );
151 $this->assertSame( 1, count( $logger->getBuffer() ) );
152 $this->assertSame( trim( $logger->getBuffer()[0][2]['error'] ), 'ThisIsStderr' );